package config

import (
	"com.go-micro.code/todolist/go-micro-todolist/user/model"
	"github.com/spf13/viper"
	"os"
	"strings"
)

var (
	Db         string
	DbHost     string
	DbPort     string
	DbUser     string
	DbPassWord string
	DbName     string
)

func Init() error {
	cwd, _ := os.Getwd()
	viper.SetConfigName("config")
	viper.SetConfigType("yml")
	viper.AddConfigPath(cwd + "/config") //获取配置文件路径

	err := viper.ReadInConfig()
	if err != nil {
		return err
	}

	LoadMysqlData()
	path := strings.Join([]string{DbUser, ":", DbPassWord, "@tcp(", DbHost, ":", DbPort, ")/", DbName, "?charset=utf8mb4&parseTime=true"}, "")
	model.Database(path)
	return nil
	//file, err := ini.Load("./conf/config.ini")
	//if err != nil {
	//	fmt.Println("配置文件读取错误，请检查文件路径:", err)
	//}
	//LoadMysqlData(file)
	//path := strings.Join([]string{DbUser, ":", DbPassWord, "@tcp(", DbHost, ":", DbPort, ")/", DbName, "?charset=utf8mb4&parseTime=true"}, "")
	//model.Database(path)
}

func LoadMysqlData() {
	Db = viper.GetString("mysql.Db")                 //file.Section("mysql").Key("Db").String()
	DbHost = viper.GetString("mysql.DbHost")         //file.Section("mysql").Key("DbHost").String()
	DbPort = viper.GetString("mysql.DbPort")         //file.Section("mysql").Key("DbPort").String()
	DbUser = viper.GetString("mysql.DbUser")         //file.Section("mysql").Key("DbUser").String()
	DbPassWord = viper.GetString("mysql.DbPassWord") //file.Section("mysql").Key("DbPassWord").String()
	DbName = viper.GetString("mysql.DbName")         //file.Section("mysql").Key("DbName").String()
}
